<?php
namespace app\admin\controller;
use app\admin\logic\FloorLogic;

/**
 * 楼层管理api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/18
 * Time: 14:12
 */
class Floor extends Base {

    public function __construct(){
        parent::__construct();
        $this->f_logic = new FloorLogic();
    }

    /**
     * 获取楼层
     * @return \think\response\Json
     */
    public function ajax_get_floor(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->f_logic->get_search_floor_maps($params['branchid']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('floor')->get_list($maps,'id,branchid,name','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$v['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branch_zw'] = $b_detail['name'];
        }

        $total_num = model('floor')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->f_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增楼层
     * @return \think\response\Json
     */
    public function ajax_add_floor(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查新增楼层规则
        $r = $this->f_logic->check_add_floor_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $f_data = [];
        $f_data['branchid'] = $params['branchid'];
        $f_data['name'] = trim($params['name']);
        $r = model('floor')->insert($f_data);
        if($r){
            return json($this->f_logic->get_response(1,'新增成功'));
        }
        return json($this->f_logic->get_response(0,'新增失败'));
    }

    /**
     * 修改楼层
     * @return \think\response\Json
     */
    public function ajax_save_floor(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改楼层规则
        $r = $this->f_logic->check_save_floor_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $f_detail = $r['data']['f_detail'];

        $f_data = [];
        $f_data['branchid'] = $params['branchid'];
        $f_data['name'] = trim($params['name']);

        $maps = [];
        $maps[] = ['id','=',$f_detail['id']];
        $r = model('floor')->force()->save($f_data,$maps);
        if($r !== false){
            return json($this->f_logic->get_response(1,'修改成功'));
        }
        return json($this->f_logic->get_response(0,'修改失败'));
    }

    /**
     * 删除楼层
     * @return \think\response\Json
     */
    public function ajax_del_floor(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $r = model('floor')->destroy($params['floorid']);
        if($r){
            return json($this->f_logic->get_response(1,'删除成功'));
        }
        return json($this->f_logic->get_response(0,'删除失败'));
    }
}